﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_EditQuestionInfo_danxuan : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerCourseInfo courseInfoManager = new ManagerCourseInfo();
    //ManagerQuestionTypeInfo questionTypeManager = new ManagerQuestionTypeInfo();

    //ManagerQuestionInfo questionInfoManager = new ManagerQuestionInfo();
    ManagerQuestion objQuestion = new ManagerQuestion();
    Course objCourse = new Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["TypeID"] = Request.QueryString["TypeID"].ToString();
            ViewState["QuestionID"] = Request.QueryString["qid"].ToString();
            ViewState["UserID"] = 10000;
            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        
        lab_AddQuestionInfo_danx_AttributeTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_AttributeTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_CourseName.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionState.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionQuestionDiffculty.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_QuestionKen.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_Ken", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_AddQuestionInfo_danx_DefaultScore.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_DefaultScore", PowerCreator.LMS.Language.LanagetItemType.Page);

        lab_ContentTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_RightAnswer.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_RightAnswer", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddOptionC.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionD.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionE.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void Pageload()
    {
        VOQuestion voQuestion = new VOQuestion();
        voQuestion = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = voQuestion;

        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        #region 绑定课程选项
        ddl_CourseList.DataSource = objCourse.Query("");
        ddl_CourseList.DataTextField = "CourseName";
        ddl_CourseList.DataValueField = "Course_Id";
        ddl_CourseList.DataBind();
        ddl_CourseList.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);

        
        for (int i = 0; i < ddl_CourseList.Items.Count; i++)
        {
            if (ddl_CourseList.Items[i].Value == voQuestion.FKQuestionCourseID.ToString())
            {
                ddl_CourseList.Items[i].Selected = true;
                continue;
            }
        }
        #endregion

        #region 绑定试题状态
        if (voQuestion.QuestionState == 1)
        {
            ddl_QuestionState.Items[0].Selected = true;
        }
        else
        {
            ddl_QuestionState.Items[1].Selected = true;
        }
        ddl_QuestionState.Items[0].Text = GetLanageValueByKey("dpdlst_ManagerQuestion_UnUse", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddl_QuestionState.Items[1].Text = GetLanageValueByKey("dpdlst_ManagerQuestion_IsUse", PowerCreator.LMS.Language.LanagetItemType.Page);

        #endregion

        //绑定难度值
        ddl_Diffculty.Items[voQuestion.QuestionDiffculty-1].Selected = true;

        txt_DefaultScore.Text = voQuestion.QuestionDefaultScore.ToString();
        txt_Ken.Text = voQuestion.QuestionKen;
        txtQuestionContent.Value = voQuestion.QuestionContent;


        #region 绑定选项
        ListItem objlist;
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";
                    //objlist.Selected = true;
                    break;
                case 1: objlist.Text = "B";
                    break;
                case 2: objlist.Text = "C";
                    break;
                case 3: objlist.Text = "D";
                    break;
                case 4: objlist.Text = "E";
                    break;
                case 5: objlist.Text = "F";
                    break;
            }
            objlist.Value = i.ToString();

            rdbtn_answer.Items.Add(objlist);
        }
        if (voQuestion.QuestionAnswer == "A")
        {
            rdbtn_answer.Items[0].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "B")
        {
            rdbtn_answer.Items[1].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "C")
        {
            rdbtn_answer.Items[2].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "D")
        {
            rdbtn_answer.Items[3].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "E")
        {
            rdbtn_answer.Items[4].Selected = true;
        }
        else
        {
            rdbtn_answer.Items[5].Selected = true;
        }
        #endregion

        #region 绑定选项内容



        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "B")
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "C")
            {
                txtOptionC.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "D")
            {
                txtOptionD.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "E")
            {
                txtOptionE.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionF.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

        #region 隐藏未选的选项
        if (dt_QuestionOption.Rows.Count == 1)
        {
            btn_AddOptionA.Visible=true;
            Pnl_B.Visible=false;
            Pnl_C.Visible=false;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 2)
        {
            btn_AddOptionA.Visible=false;
            Pnl_B.Visible=true;

            Pnl_C.Visible=false;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 3)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 4)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count ==5)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 6)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            btn_AddOptionE.Visible = false;
            Pnl_F.Visible = true;
        }
        #endregion
    }
    private bool PageCheck()
    {
        if (string.IsNullOrEmpty(txt_DefaultScore.Text.Trim()) | string.IsNullOrEmpty(txt_Ken.Text.Trim()) | string.IsNullOrEmpty(txtQuestionContent.Value.Trim()))
        {
            Script.Alert(this, "msg", GetLanageValueByKey("msg_AddQuestionInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (!PageValidate.IsNumber(txt_DefaultScore.Text.Trim()))
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_QuestionToIn_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (ddl_CourseList.SelectedValue == "-1")
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_QuestionToIn_checkmsg3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }
    private string ConvertOptionID(int Index)
    {
        string OptionID = string.Empty;
        switch (Index)
        {
            case 0: OptionID = "A";
                break;
            case 1: OptionID = "B";
                break;
            case 2: OptionID = "C";
                break;
            case 3: OptionID = "D";
                break;
            case 4: OptionID = "E";
                break;
            case 5: OptionID = "F";
                break;
        }
        return OptionID;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }

        ArrayList list = new ArrayList();

        ArrayList Optionlist = new ArrayList();
        Optionlist.Add(txtOptionA.Text.Trim());
        Optionlist.Add(txtOptionB.Text.Trim());
        Optionlist.Add(txtOptionC.Text.Trim());
        Optionlist.Add(txtOptionD.Text.Trim());
        Optionlist.Add(txtOptionE.Text.Trim());
        Optionlist.Add(txtOptionF.Text.Trim());


        for (int i = 0; i < rdbtn_answer.SelectedIndex + 1; i++)
        {
            string OptionID = ConvertOptionID(i);

            if (Optionlist[i].ToString() == "")
            {
                Script.Alert(Page, "msg", OptionID + GetLanageValueByKey("msg_AddQuestionInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }
        }
        for (int i = 0; i < Optionlist.Count; i++)
        {
            string OptionID = ConvertOptionID(i);
            if (Optionlist[i].ToString() != "")
            {
                list.Add(OptionID + "、" + Optionlist[i].ToString());
            }
        }

        #region 删除选项
        
        DataTable dt_QuestionOption = (DataTable)ViewState["dt_QuestionOption"];
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
            objQuestion.DeleteQuestionOption(int.Parse(optionID));

        }
        #endregion

        VOQuestion voQuestion = new VOQuestion();
        voQuestion = (VOQuestion)ViewState["objQuestionInfo"];
        voQuestion.FKQuestionCourseID = int.Parse(ddl_CourseList.SelectedValue);
        voQuestion.QuestionCreateID = int.Parse(ViewState["UserID"].ToString());
        //info.DateCreated = DateTime.Now;

        voQuestion.QuestionAnswer = rdbtn_answer.SelectedItem.Text;
        voQuestion.QuestionDefaultScore = int.Parse(txt_DefaultScore.Text);
        voQuestion.QuestionDiffculty = int.Parse(ddl_Diffculty.SelectedValue);
        voQuestion.QuestionKen = txt_Ken.Text;


        voQuestion.QuestionContent = txtQuestionContent.Value;


        voQuestion.QuestionState = int.Parse(ddl_QuestionState.SelectedValue);         //禁用1,启用2

        VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();
        
        for (int i = 0; i < list.Count; i++)
        {
            objQuestionOptionInfo.FKQuestionOptionQuestionID = voQuestion.QuestionID;
            objQuestionOptionInfo.QuestionOptionContent = list[i].ToString();
            objQuestionOptionInfo.QuestionOptionCreateID = int.Parse(ViewState["UserID"].ToString());
            objQuestionOptionInfo.QuestionOptionCreateTime = DateTime.Now;
            objQuestion.CreateQuestionOption(objQuestionOptionInfo);
        }


        if (objQuestion.UpdateQuetion(voQuestion))//更新试题信息
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerQuestionInfo.aspx");
        }
        else
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerQuestionInfo.aspx");
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerQuestionInfo.aspx");
    }
    protected void btn_AddOptionA_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "B";
        objlist.Value = "1";
        rdbtn_answer.Items.Add(objlist);
        Pnl_B.Visible = true;
        btn_AddOptionA.Visible = false;
    }

    protected void btn_AddOptionB_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "C";
        objlist.Value = "2";
        rdbtn_answer.Items.Add(objlist);
        Pnl_C.Visible = true;
        btn_AddOptionB.Visible = false;
    }
    protected void btn_AddOptionC_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "D";
        objlist.Value = "3";
        rdbtn_answer.Items.Add(objlist);
        Pnl_D.Visible = true;
        btn_AddOptionC.Visible = false;
    }
    protected void btn_AddOptionD_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "E";
        objlist.Value = "4";
        rdbtn_answer.Items.Add(objlist);
        Pnl_E.Visible = true;
        btn_AddOptionD.Visible = false;
    }
    protected void btn_AddOptionE_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "F";
        objlist.Value = "5";
        rdbtn_answer.Items.Add(objlist);
        Pnl_F.Visible = true;
        btn_AddOptionE.Visible = false;
    }

}
